package com.wno704.quartz.task;

import com.wno704.quartz.entity.Job;
import com.wno704.quartz.service.JobService;
import com.wno704.system.util.ShellUtil;
import com.wno704.system.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;


/**
 * @ClassName ShellTask
 * @Description TODO
 * @Author wno704
 * @Date 2022/3/7 22:10
 * @Version 1.0
 */
@Slf4j
@Component
public class ShellTask {

    @Autowired
    private JobService jobService;

    public void exceCmd(String params) throws Exception {
        Job job = jobService.findJob(Long.parseLong(params));
        log.info("{}-{}----start", job.getRemark(), "exceCmd");
        log.info("{}----{}----{}----{}----{}----{}----{}", params, job.getTemplateSrcName(), job.getTemplateDstName(), job.getTemplateDstTwoName(), job.getSrcCommd(), job.getDstCommd(), job.getDstTwoCommd());

        String dstCmd = job.getDstCommd();

        String[] ipPort = job.getTemplateDstName().split(":");
        String[] userCodePasswd = job.getTemplateDstTwoName().split(":");

        String res = ShellUtil.execCommod(ipPort[0], Integer.parseInt(ipPort[1]), userCodePasswd[0], userCodePasswd[1], dstCmd);
        if (job.getTransType().equals("1")) {
            JdbcTemplate jdbcTemplateSrc = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateSrcName());
            String srcCmd = job.getSrcCommd();
            if (!(jdbcTemplateSrc == null || jdbcTemplateSrc.equals(""))) {
                jdbcTemplateSrc.update(srcCmd, new Object[]{ipPort[0], res});
            }
        }

        log.info("{}-{}----end", job.getRemark(), "exceCmd");
    }

}
